package com.nsl.my_youlai.mystar.easy_excel.check;


import com.alibaba.excel.annotation.ExcelProperty;
import com.nsl.my_youlai.mystar.easy_excel.NslUtilEasyExcel;
import com.nsl.my_youlai.mystar.easy_excel.check.annotations.*;
import com.nsl.my_youlai.mystar.easy_excel.exception.ExcelNullValueException;
import com.nsl.my_youlai.mystar.easy_excel.model.ExcelReadCheck;
import com.nsl.my_youlai.mystar.easy_excel.model.ExcelStringRecorde;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NslUtilExcelValid {
    public  static Pattern intPattern = Pattern.compile("^-?[1-9]?\\d*$|0");
    public  static Pattern numberPattern = Pattern.compile("^(-?\\d+)(\\.\\d+)?$");
    /**
     * Excel导入字段校验
     *
     * @param object 校验的JavaBean 其属性须有自定义注解
     */
    public static void valid(Object object) throws ExcelNullValueException {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            //设置可访问
            field.setAccessible(true);
            //属性的值
            Object fieldValue = null;
            try {
                fieldValue = field.get(object);
            } catch (IllegalAccessException e) {
                throw new RuntimeException("导入参数检查失败");
            }
            //是否包含必填校验注解
            boolean isNotNull = field.isAnnotationPresent(ExcelProperty.class);


        }
    }

    public static <T extends ExcelStringRecorde>void validList(List<T> recordList,List<ExcelReadCheck> checkList){
        for (ExcelStringRecorde excelStringRecorde : recordList) {
            valid(excelStringRecorde, checkList);
        }
    }

    public static void valid(ExcelStringRecorde record, List<ExcelReadCheck> checkList){
        Field[] fields = record.getClass().getDeclaredFields();
        Map<String,String> prop2ExcelSheetHeadMap=NslUtilEasyExcel.getProp2ExcelSheetHeadMap(record.getClass());

        for (Field field : fields) {
            //设置可访问
            field.setAccessible(true);
            //属性的值
            Object fieldValue = null;
            try {
                fieldValue = field.get(record);
            } catch (IllegalAccessException e) {
                throw new RuntimeException("导入参数检查失败");
            }
            //是否包含必填校验注解
            boolean isExcelProperty = field.isAnnotationPresent(ExcelProperty.class);
            Annotation[] annotations=field.getAnnotations();

            if(isExcelProperty){
                for (Annotation annotation : annotations) {
                    if(annotation instanceof ExcelProperty){
                        continue;
                    }

                    if(annotation instanceof ExcelNotNull){
                        if(Objects.isNull(fieldValue)){
                            ExcelReadCheck readCheck=new ExcelReadCheck();
                            readCheck.setRowIndex(record.getRowIndex());
                            readCheck.setHeadString(prop2ExcelSheetHeadMap.get(field.getName()));
                            readCheck.setExcelInputString(null);
                            readCheck.setTitle("输入不能为空值");
                            readCheck.setMessage(readCheck.getHeadString()+"不能为空值");
                            checkList.add(readCheck);
                            break;
                        }
                    }

                    if(fieldValue==null){
                        break;
                    }

                    String strValue=fieldValue.toString();

                    if(annotation instanceof ExcelInt){
                        ExcelReadCheck readCheck=new ExcelReadCheck();
                        readCheck.setRowIndex(record.getRowIndex());
                        readCheck.setHeadString(prop2ExcelSheetHeadMap.get(field.getName()));
                        Matcher matcher= intPattern.matcher(strValue);
                       if(!matcher.find()){
                           readCheck.setExcelInputString(strValue);
                           readCheck.setTitle("输入值必须为整数");
                           readCheck.setMessage("输入:"+strValue+"不为整数");
                           checkList.add(readCheck);
                       }
                    }

                    if(annotation instanceof ExcelNumber){
                        ExcelReadCheck readCheck=new ExcelReadCheck();
                        readCheck.setRowIndex(record.getRowIndex());
                        readCheck.setHeadString(prop2ExcelSheetHeadMap.get(field.getName()));
                        Matcher matcher= numberPattern.matcher(strValue);
                        if(!matcher.find()){
                            readCheck.setExcelInputString(strValue);
                            readCheck.setTitle("输入值必须为数值");
                            readCheck.setMessage("输入:"+strValue+"不为数值");
                            checkList.add(readCheck);
                        }
                    }

                    if(annotation instanceof ExcelValueRange){
                        ExcelReadCheck readCheck=new ExcelReadCheck();
                        readCheck.setRowIndex(record.getRowIndex());
                        readCheck.setHeadString(prop2ExcelSheetHeadMap.get(field.getName()));
                        readCheck.setExcelInputString(strValue);
                        ExcelValueRange excelValueRange= (ExcelValueRange) annotation;
                        List rangList=Arrays.asList(excelValueRange.value());
                        if(!rangList.contains(strValue)){
                            readCheck.setTitle("输入非指定枚举值");
                            readCheck.setMessage("输入:"+strValue+" 不在 "+rangList+" 范围内");
                            checkList.add(readCheck);
                        }

                    }
                }
            }
        }
    }


}